package com.zhengdongzulin.zuke.app.main.mine

import android.view.View
import com.lxj.androidktx.core.*
import com.zhengdongzulin.zuke.R
import com.zhengdongzulin.zuke.app.data.MessageData
import com.zhengdongzulin.zuke.base.*
import com.zhengdongzulin.zuke.databinding.ZkActivityMessageBinding
import com.zhengdongzulin.zuke.ext.*


/**
 * jeff
 * 我的消息
 * 2021-03-27 17:50:47
 */
class ZKMessageActivity : BaseActivity<MessageVm, ZkActivityMessageBinding>() {

    var mData = mutableListOf<MessageData>()

    override fun getLayoutResId(): Int = R.layout.zk_activity_message

    override fun initVM(): MessageVm = getVM(MessageVm::class.java)

    override fun initView() {
        setTitleContent("我的消息")
        mView.statusView.showLoading()
        mView.contentView.setEnableLoadMore(false)
        mViewModel.get_message_list()
    }

    override fun initData() {
        //1逾期账单，2保洁 3维修 4投诉
        mView.recyclerView.vertical()
            .bindData(mData, R.layout.item_message) { holder, t, position ->
                holder.setText(R.id.title_name, t.title)
                holder.loadCircleImage(
                    R.id.photo, when (t.type) {
                        "1" -> {
                            R.mipmap.message_03
                        }
                        "2" -> {
                            R.mipmap.message_04
                        }
                        "3" -> {
                            R.mipmap.message_01
                        }
                        else -> {
                            R.mipmap.message_02
                        }
                    }, R.mipmap.mine_empty_photo
                )
                holder.setText(R.id.time, t.createTime)
                holder.setText(R.id.desc, t.context)
                var v = holder.getView<View>(R.id.reader)
                if (t.status == "0") {//0 未读 1 已读
                    v.visible()
                } else {
                    v.gone()
                }
            }.itemClick { data: List<MessageData>, holder, position ->
            mViewModel.update_status(data[position].id)
        }

    }

    override fun initListener() {
        mView.contentView.setOnRefreshListener {
            mViewModel.get_message_list()
        }
    }

    override fun startObserve() {
        mViewModel.get_message_list.observe(this, androidx.lifecycle.Observer {
            mData.clear()
            if (it != null) {
                if (it.messageInfoList != null && it.messageInfoList.size > 0) {

                    mData.addAll(it.messageInfoList)
                }
                mView.recyclerView.adapter!!.notifyDataSetChanged()
                if (mData.size > 0) {
                    mView.statusView.showContent()
                } else {
                    mView.statusView.showEmpty()
                }
            } else {
                mView.statusView.showError()
            }
            mView.contentView.finishRefresh()
        })
        mViewModel.get_message_list.state.observe(
            this,
            getObserver(mViewModel.get_message_list)
        )
        mViewModel.update_status.observe(this, androidx.lifecycle.Observer {
            mViewModel.get_message_list()
        })
        mViewModel.get_message_list.state.observe(
            this,
            getObserver(mViewModel.get_message_list)
        )
    }

}